package common;

import org.apache.commons.cli.*;

import java.util.logging.Logger;


public class CLIReader {

    private static final Logger logger = Logger.getLogger("common.CLIReader");

    private CLIReader(){}

    public static CLIParameters readCLI(String[] args) {
        Options options = new Options();
        options.addOption("h", false, "This option show you information about available options of application");

        Option draw = OptionBuilder.withArgName("HDF5file" + " dataSet" + " xSize" + " ySize")
                .hasArgs(4)
                .withValueSeparator()
                .isRequired()
                .withDescription("Use this option for visualization results")
                .create("draw");
        options.addOption(draw);
        CommandLineParser cliParser = new GnuParser();

        CommandLine line = null;
        try {
            line = cliParser.parse(options, args);
        } catch (ParseException exp) {
            System.err.println("Parsing failed.  Reason: " + exp.getMessage());
        }

        if (line.hasOption("h")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("HFDTools", options);
            System.exit(0);
        }

        String[] optionValues = line.getOptionValues("draw");
        String fileName = optionValues[0];
        String dataSetName = optionValues[1];
        int xSize = Integer.valueOf(optionValues[2]);
        int ySize = Integer.valueOf(optionValues[3]);

        logger.fine("File name:" + fileName);
        logger.fine("data name:" + dataSetName);
        logger.fine("x:" + xSize);
        logger.fine("y:" + ySize);
        return new CLIParameters(fileName, dataSetName, xSize, ySize);
    }

}
